﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace LCARSLibrary
{
    public static class RoundedRectangle
    {
        public static void CreateRoundRectPath(GraphicsPath fillPath, int x1, int y1, int x2, int y2, int radius)
        {
            CreateRoundRectPath(fillPath, x1, y1, x2, y2, radius, Corner.All, new Padding(0));
        }

        public static void CreateRoundRectPath(GraphicsPath fillPath, int x1, int y1, int x2, int y2, int radius, Corner corner)
        {
            CreateRoundRectPath(fillPath, x1, y1, x2, y2, radius, corner, new Padding(0));
        }

        public static void CreateRoundRectPath(GraphicsPath fillPath, int x1, int y1, int x2, int y2, int radius, Padding padding)
        {
            CreateRoundRectPath(fillPath, x1, y1, x2, y2, radius, Corner.All, padding);
        }

        public static void CreateRoundRectPath(GraphicsPath fillPath, int x1, int y1, int x2, int y2, int radius, Corner corners, Padding padding)
        {
            int correction = 1;

            fillPath.Reset();
            fillPath.StartFigure();

            fillPath.AddLine(((corners & Corner.TopLeft) == Corner.TopLeft) ? x1 + radius + padding.Left : x1 + padding.Left,
                y1 + padding.Top,
                ((corners & Corner.TopRight) == Corner.TopRight) ? x2 - radius - padding.Right - correction : x2 - padding.Right - correction,
                y1 + padding.Top);

            if (radius > 0 && (corners & Corner.TopRight) == Corner.TopRight)
                fillPath.AddArc(x2 - radius - padding.Right - correction, y1 + padding.Top, radius, radius, 270, 90);



            fillPath.AddLine(x2 - padding.Right - correction,
                ((corners & Corner.TopRight) == Corner.TopRight) ? y1 + radius + padding.Top : y1 + padding.Top,
                x2 - padding.Right - correction,
                ((corners & Corner.BottomRight) == Corner.BottomRight) ? y2 - radius - padding.Bottom - correction : y2 - padding.Bottom - correction);

            if (radius > 0 && (corners & Corner.BottomRight) == Corner.BottomRight)
                fillPath.AddArc(x2 - radius - padding.Right - correction, y2 - radius - padding.Bottom - correction, radius, radius, 0, 90);



            fillPath.AddLine(((corners & Corner.BottomRight) == Corner.BottomRight) ? x2 - radius - padding.Right - correction : x2 - padding.Right - correction,
                y2 - padding.Bottom - correction,
                ((corners & Corner.BottomLeft) == Corner.BottomLeft) ? x1 + radius + padding.Left : x1 + padding.Left,
                y2 - padding.Bottom - correction);

            if (radius > 0 && (corners & Corner.BottomLeft) == Corner.BottomLeft)
                fillPath.AddArc(x1 + padding.Left, y2 - radius - padding.Bottom - correction, radius, radius, 90, 90);



            fillPath.AddLine(x1 + padding.Left,
                ((corners & Corner.BottomLeft) == Corner.BottomLeft) ? y2 - radius - padding.Bottom - correction : y2 - padding.Bottom - correction,
                x1 + padding.Left,
                ((corners & Corner.TopLeft) == Corner.TopLeft) ? y1 + radius + padding.Top : y1 + padding.Top);

            if (radius > 0 && (corners & Corner.TopLeft) == Corner.TopLeft)
                fillPath.AddArc(x1 + padding.Left, y1 + padding.Top, radius, radius, 180, 90);


            fillPath.CloseFigure();
        }
    }
}
